<template>
  <div class="deviceCategory-page">
    <div class="search-field">
      <el-form :model="searchForm" inline>
        <el-form-item label="类型名称" prop="name">
          <el-input
            placeholder="请输入类型名称"
            v-model="searchForm.name"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="SPU分类" prop="name">
          <el-select
            style="width: 240px"
            v-model="searchForm.categoryId"
            placeholder="选择SPU分类"
          >
            <el-option
              v-for="option in productCategoryList"
              :key="option.id"
              :label="option.name"
              :value="option.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <div>
            <el-button type="primary" @click="startSearch"> 查询 </el-button>
            <el-button @click="startClear"> 重置 </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-pagination-content">
      <div class="btns-fun-field">
        <!-- v-if="userPermission.addUserPerm" -->
        <el-button
          type="primary"
          @click.prevent="showAddEditDialog"
          :icon="Plus"
        >
          新增设备类型
        </el-button>
      </div>
      <div class="table-field">
        <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%"
          v-loading="tableLoading"
        >
          <el-table-column prop="number" label="编码" width="120" />
          <el-table-column prop="url" label="图片" width="100">
            <template #default="scope">
              <el-image
                style="width: 60px; height: 60px"
                :src="scope.row.url"
                :preview-src-list="[scope.row.url]"
                fit="cover"
                :preview-teleported="true"
              >
              </el-image>
            </template>
          </el-table-column>

          <el-table-column prop="name" label="名称" width="120" />
          <el-table-column prop="categoryName" label="SPU分类" width="100" />
          <el-table-column prop="brandName" label="品牌" width="90" />
          <el-table-column prop="model" label="型号" width="150" />
          <el-table-column label="功能" width="90">
            <template #default="">
              <div>全自动</div>
            </template>
          </el-table-column>
          <el-table-column prop="size" label="尺寸" width="120" />
          <el-table-column prop="capacity" label="容量" width="90" />
          <el-table-column prop="weight" label="重量" width="90" />

          <el-table-column prop="mark" label="描述" width="200" />
          <el-table-column prop="createTime" label="创建时间" width="170" />
          <el-table-column label="操作" min-width="220px" fixed="right">
            <template #default="scope">
              <!-- v-if="rolePermission.editRolePerm" -->
              <el-button
                type="success"
                size="small"
                @click.prevent="showAddEditDialog(scope.row, 2)"
                >编辑</el-button
              >
              <!-- v-if="rolePermission.removeRolePerm" -->
              <el-button
                type="danger"
                size="small"
                @click.prevent="showRemoveDialog(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-field">
        <el-pagination
          v-model:current-page="formData.pageIndex"
          v-model:page-size="formData.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="formData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <DeviceCategoryAdd v-model="addUpdateRef"></DeviceCategoryAdd>
    <DeviceCategoryRemove v-model="removeRef"></DeviceCategoryRemove>
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  getDeviceCategoryPagination,
  getDeviceCategoryAllList,
} from '@/api/dms-device-category.api'
import {
  getProductCategoryPagination,
  getProductCategoryAllList,
} from '@/api/dms-product-category.api'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
// import { useUserPermission } from "@/utils/permission.utils";
import DeviceCategoryRemove from './DeviceCategoryRemove.vue'
import DeviceCategoryAdd from './DeviceCategoryAdd.vue'
import { getCanUseCode } from '@/api/fms-code-dict.api'

const titleName = `设备类型`
// const userPermission = useUserPermission();

/****
 * =============== 新增、修改相关操作 =====================
 */
const addUpdateRef = reactive({
  visible: false,
  disabled: false,
  title: `设备类型`,
  curSelectRow: {},
  roleList: [],
  form: {},
  rules: {},
  productCategoryList: [],
  refreshDataList: () => {
    getDataList()
  },
})

const requestCanUseCode = () => {
  getCanUseCode({ type: 101 })
    .then((res: any) => {
      console.log('getCanUseCode: ', res)
      addUpdateRef.form.number = res
    })
    .catch((err) => {
      ElMessage.error(err.msg)
    })
}

const showAddEditDialog = (item: any, type: number) => {
  //新增
  if (item.target) {
    let form = {
      id: null,
      categoryId: '',
      number: '',
      name: '',
      brandName: '',
      model: '',
      size: '',
      capacity: '',
      url: '',
      mark: '',
      func: '1',
      weight: '',
      disabled: false,
    }
    addUpdateRef.visible = true
    addUpdateRef.disabled = false
    addUpdateRef.form = form
    requestCanUseCode()
    return
  }

  let form = {
    id: item.id,
    categoryId: item.categoryId,
    number: item.number,
    name: item.name,
    brandName: item.brandName,
    model: item.model,
    size: item.size,
    capacity: item.capacity,
    url: item.url,
    mark: item.mark,
    weight: item.weight,
    func: '1',
    disabled: false,
  }
  addUpdateRef.visible = true
  addUpdateRef.disabled = true
  addUpdateRef.form = form
}

/****
 * =============== 删除操作 =====================
 */
const removeRef = reactive({
  visible: false,
  title: `设备类型`,
  curSelectRow: {},
  refreshDataList: () => {
    getDataList()
  },
})
const showRemoveDialog = (item: any) => {
  removeRef.visible = true
  removeRef.curSelectRow = item
}

/****
 * =============== 搜索、表格、分页 =====================
 */
const formData = reactive({
  pageIndex: 1,
  pageSize: 10,
  total: 0,
})

const searchForm = ref({
  number: '',
  name: '',
  categoryId: '',
})

const tableLoading = ref(false)
const tableData = ref([])
/***
 * 开始搜索
 */
const startSearch = () => {
  formData.pageIndex = 1
  getDataList()
}

/***
 * 重置搜索条件
 */
const startClear = () => {
  formData.pageIndex = 1
  let value = searchForm.value
  Object.keys(value).forEach((key) => {
    ;(value as any)[key] = ''
  })
  getDataList()
}

const handleSizeChange = (value: number) => {
  formData.pageSize = value
  getDataList()
}

const handleCurrentChange = (value: number) => {
  formData.pageIndex = value
  getDataList()
}

const getDataList = () => {
  let searchCondition: any = {}

  let number = searchForm.value.number
  if (number) {
    searchCondition.number = number
  }
  let name = searchForm.value.name
  if (name) {
    searchCondition.name = name
  }
  let categoryId = searchForm.value.categoryId
  if (categoryId) {
    searchCondition.categoryId = categoryId
  }

  tableLoading.value = true
  getDeviceCategoryPagination({
    pageSize: formData.pageSize,
    pageIndex: formData.pageIndex,
    ...searchCondition,
  })
    .then(function (res: any) {
      tableLoading.value = false
      console.log(res)
      tableData.value = res.list || []
      formData.total = Number(res.total)
    })
    .catch((error) => {
      console.log(error)
      tableLoading.value = false
    })
}

const productCategoryList = ref([])
const requestProductCategoryAllList = () => {
  getProductCategoryAllList({})
    .then((res: any) => {
      productCategoryList.value = res || []
      addUpdateRef.productCategoryList = productCategoryList.value
    })
    .catch((err) => {
      console.log(err)
    })
}

onMounted(() => {
  requestProductCategoryAllList()
  getDataList()
})
</script>
<style lang="scss" scoped>
.deviceCategory-page {
  height: 100%;
}

.btns-fun-field {
  margin-bottom: 20px;
}

.pagination-field {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}

.btn-inner {
  display: flex;
  justify-content: center;
  color: #fff;
}

.tag-field {
  .el-tag {
    margin-right: 5px;
    &:last-child {
      margin-right: 0;
    }
  }
}

.search-field {
  margin-bottom: 12px;
  background: #fff;
  padding: 20px 20px 0px 20px;
  border-radius: 8px;
}

.deviceCategory-page-content {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}
</style>
